package com.lxb.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxb.commonutils.R;
import com.lxb.eduservice.client.VodClient;
import com.lxb.eduservice.entity.EduVideo;
import com.lxb.eduservice.mapper.EduVideoMapper;
import com.lxb.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxb.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * 小节 课程视频 业务实现层
 *
 * @author lixuebin
 * @since 2022-11-06
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void removeVideoByCourseId(String courseId) {

        List<String> vodIds = new ArrayList();

        //删除小节，删除对应视频文件
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        //获取小节集合
        List<EduVideo> videoList = baseMapper.selectList(queryWrapper);

        //判断集合为空
        if (videoList.size() != 0) {
            for (EduVideo video : videoList) {
                //保存小节视频id
                if (StringUtils.isNotEmpty(video.getVideoSourceId())) {
                    vodIds.add(video.getVideoSourceId());
                }
            }
            //1.删除小节视频，调用远程接口
            R r = vodClient.deleteBath(vodIds);
//            if (r.getCode() == 20001){
//                throw new GuliException(20001,r.getMessage());
//            }
        }
        //2.删除小节
        baseMapper.delete(queryWrapper);
    }
}
